package com.example.administrator.gamecat2;

import android.content.DialogInterface;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioGroup;

import com.example.administrator.gamecat2.fragment.CatHomeFragment;
import com.example.administrator.gamecat2.fragment.MessageFragment;
import com.example.administrator.gamecat2.fragment.PlayFragment;
import com.example.administrator.gamecat2.fragment.UnionFragment;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{

    private RadioGroup rgBottom;
    private FragmentManager manager;
    //声明四个fragment
    private MessageFragment msgFragment;
    private UnionFragment unionFragment;
    private PlayFragment playFragment;
    private CatHomeFragment catFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化view
        initView();
        //初始化数据
        initData();
    }

    private void initData() {
        //初始化操作
        selection(0);
    }

    private void selection(int i) {
        //开启事物
        FragmentTransaction transaction = manager.beginTransaction();
        //隐藏的fragment
        hindFragment(transaction);
        switch (i){
            case 0:
                //如果为空则创建
                if (msgFragment == null){
                    Log.i("TAG","0000000000");
                    msgFragment = new MessageFragment();
                    transaction.add(R.id.main_framelayout_content,msgFragment);
                }else {
                    //否则显示  其他的隐藏
                    transaction.show(msgFragment);
                }
                break;
            case 1:
                //如果为空则创建
                if (unionFragment == null){
                    Log.i("TAG","111111111111111111");
                    unionFragment = new UnionFragment();
                    transaction.add(R.id.main_framelayout_content,unionFragment);
                }else {
                    //否则显示  其他的隐藏
                    transaction.show(unionFragment);
                }
                break;
            case 2:
                //如果为空则创建
                if (playFragment == null){
                    Log.i("TAG","222222222222222222");
                    playFragment = new PlayFragment();
                    transaction.add(R.id.main_framelayout_content,playFragment);
                }else {
                    //否则显示  其他的隐藏
                    transaction.show(playFragment);
                }
                break;
            case 3:
                //如果为空则创建
                if (catFragment == null){
                    Log.i("TAG","33333333333333333333");
                    catFragment = new CatHomeFragment();
                    transaction.add(R.id.main_framelayout_content,catFragment);
                }else {
                    //否则显示  其他的隐藏
                    transaction.show(catFragment);
                }
                break;
        }
        transaction.commit();
    }

    //要隐藏的fragment
    private void hindFragment(FragmentTransaction transaction) {
        //如果fragment不为空则保存当前的fragment状态
        if (msgFragment != null){
            transaction.hide(msgFragment);
        }
        if (unionFragment != null){
            transaction.hide(unionFragment);
        }
        if (playFragment != null){
            transaction.hide(playFragment);
        }
        if (catFragment != null){
            transaction.hide(catFragment);
        }
    }

    //初始化布局
    private void initView() {
        rgBottom = (RadioGroup) findViewById(R.id.main_radiogroup);
        //设置radiogroup的监听事件
        rgBottom.setOnCheckedChangeListener(this);
        //设置布局管理器
        manager = getSupportFragmentManager();

    }

    //设置radiogroup的监听事件
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case R.id.tab_message:
                selection(0);
                break;
            case R.id.tab_union:
                selection(1);
                break;
            case R.id.tab_play:
                selection(2);
                break;
            case R.id.tab_cathome:
                selection(3);
                break;
        }
    }
    //返回键的处理

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK){
            //弹出提示的dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("确定退出?");
            //设置确定按钮的监听事件
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    MainActivity.this.finish();
                }
            });
            //设置取消的监听
            builder.setNegativeButton("取消",null);
            builder.show();
        }
        return super.onKeyDown(keyCode, event);
    }
}
